package com.example.myapplication;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.example.utils.SystemUtil;

public class SettingActivity extends AppCompatActivity implements View.OnClickListener {
    private final static  String PRINTER_B11 = "B11";////蓝牙80
    public final static  String PRINT_TYPE = "printType";
    private String printerType = "";  // 当前打印机类型
    private ActivityResultLauncher<Intent> buletoothForResult;
    private ActivityResultLauncher<Intent> con_buletoothForResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        findViewById(R.id.B11Btn).setOnClickListener(this);




        //回调
        buletoothForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result != null && result.getResultCode() == Activity.RESULT_OK){
                chooseDevice();
            }else {
                SystemUtil.showAlertDialog(this,"蓝牙没有启动",null);
            }
        });

        con_buletoothForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            if (result != null && result.getResultCode() == Activity.RESULT_OK){
                chooseDevice();
            }else {
                SystemUtil.showAlertDialog(this,"蓝牙没有启动",null);
            }
        });

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.B11Btn){
            printerType = PRINTER_B11;
            chooseDevice();
        }
    }

    /**
     * 搜设备
     */
    private void chooseDevice() {
        //判断设备蓝牙是否开启
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!bluetoothAdapter.isEnabled()){//蓝牙未开启
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            buletoothForResult.launch(intent);
        }else {//蓝牙已开启
            Intent intent = new Intent(this, BluetoothDeviceListActivity.class);
            intent.putExtra(PRINT_TYPE,printerType);
            con_buletoothForResult.launch(intent);
        }
    }

}